/*
 * Albareth - an action roleplaying game.
 * Copyright (C) 2008 Jens Anuth
 *
 * This program is free software; you can redistribute it and/or
 * modify it under the terms of the GNU General Public License
 * as published by the Free Software Foundation; either version 2
 * of the License, or (at your option) any later version.
 * 
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 * 
 * You should have received a copy of the GNU General Public License
 * along with this program; if not, write to the Free Software
 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA
 *  02110-1301, USA.
 * 
 * $Id: BuffFrame.java 43 2008-05-13 16:52:12Z jens464 $
*/
package albareth.frontend.slick.ui;

import java.util.Iterator;
import java.util.List;

import org.newdawn.slick.GameContainer;
import org.newdawn.slick.Graphics;
import org.newdawn.slick.Image;
import org.newdawn.slick.SlickException;

import albareth.core.buff.Buff;
import albareth.core.entity.PlayerCharacter;
import albareth.frontend.TileCollection;
import albareth.frontend.slick.SlickResources;

public class BuffFrame extends UIElement
{
    float _scale = 2.0f;
    PlayerCharacter _player;
    int _columns;
    int _rows;
    int _iconWidth;
    int _iconHeight;

    
    public BuffFrame(PlayerCharacter player, int columns, int rows)
    {
        _player = player;
        _columns = columns;
        _rows = rows;
        setSize(
                _columns * (int)(TileCollection.getDefaultTileWidth() * _scale),
                _rows * (int)(TileCollection.getDefaultTileHeight() * _scale)
        );
        _iconWidth = (int)(TileCollection.getDefaultTileWidth() * _scale);
        _iconHeight = (int)(TileCollection.getDefaultTileHeight() * _scale);

    }

    public void render(Graphics g, GameContainer container, int screenX,
            int screenY) throws SlickException
    {
        List buffs = _player.getBuffs();
        
        if (buffs == null) {
            return;
        }
        
        for (int i = 0; i < buffs.size(); i++)
        {
            Buff buff = (Buff)buffs.get(i);
            Image buffImg = SlickResources.getTileImage(buff.getTile());
            int x =(i % _columns) * _iconWidth;
            int y =(i / _columns) * _iconHeight;
            buffImg.draw(screenX + x, screenY + y, _scale);
            if (buff.isBeneficial()) {
                SlickResources.BUFF_BENEFICAL.draw(screenX + x, screenY + y, _scale);
            } else {
                SlickResources.BUFF_HARMFUL.draw(screenX + x, screenY + y, _scale);
            }
        }
    }

    public String getTooltipText(int x, int y)
    {
        List buffs = _player.getBuffs();
        if (buffs == null) {
            return null;
        }
        
        int ix = x / _iconWidth;
        int iy = y / _iconHeight;
        int index = iy + _columns * ix;
        
        if (index >= buffs.size()) {
            return null;
        }
        
        return ((Buff)buffs.get(index)).getName();
    }

}
